﻿using System.Linq;
using System.Windows.Forms;
using Andromeda.Global.Utils;
using Andromeda.Global.Common;
using Andromeda.Base.Application.UI.Windows.Forms;

namespace ForgeIt.Internal
{
    /// <summary>
    /// Provides static helper methods to be shared with multiple components
    /// </summary>
    public static class Helper
    {
        /// <summary>
        /// Allows to display an OperationResult object in a visually treated way
        /// </summary>
        /// <param name="result">The OperationResult object to display</param>
        public static void DisplayOperationResult(OperationResult result)
        {
            DisplayOperationResult(result, false);
        }

        /// <summary>
        /// Allows to display an OperationResult object in a visually treated way
        /// </summary>
        /// <param name="result">The OperationResult object to display</param>
        /// <param name="showOk">Should the result be displayed even if there are no errors</param>
        public static void DisplayOperationResult(OperationResult result, bool showOk)
        {
            if (result == null) return;

            if (result.HasErrors && result.Errors.Count(item => item.ErrorException != null) > 0)
            {
                ExceptionDialogForm.ShowDialog(result);
            }
            else if (result.HasErrors && result.Errors.Count(item => item.ErrorException != null) == 0)
            {
                MessageBox.Show(result.ToString(), Utilities.ReadConfigValue("ErrorLabel", "Erros encontrados"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (showOk)
            {
                // Display ok to user
                // TODO: Enhance this with custom form, and inner self navigation (tree like OperationResult.Results)
                MessageBox.Show(result.ToString(), result.OperationName, MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
    }
}
